package com.fh.binarysearch;

/**
 * T167. 两数之和 II - 输入有序数组
 * @author fuhao
 * @date 2025/10/14 21:59
 */
public class T167TwoSum {
    class Solution {
        public int[] twoSum(int[] numbers, int target) {
            int left = 0;
            int right = numbers.length - 1;

            while (left < right) {
                int sum = numbers[left] + numbers[right];
                if (sum == target) {
                    return new int[]{left + 1, right + 1};
                } else if (sum < target) {
                    left++; // 和太小，左指针右移以增大和
                } else {
                    right--; // 和太大，右指针左移以减小和
                }
            }
            // 题目保证有唯一解，理论上不会执行到此处
            return new int[]{-1, -1};
        }
    }
}
